import Ember from 'ember';
import BaseItem from '../ui/base-ui-item';
import Changeset from 'ember-changeset';
import CustomerValidations from '../../validations/customer';
import lookupValidator from 'ember-changeset-validations';
//const {sexTypeMale,sexTypeFemale} = Constants;
export default BaseItem.extend(CustomerValidations, {
    constants: Constants,
    statusService: Ember.inject.service("current-status"),
    store: Ember.inject.service("store"),
    dateService: Ember.inject.service("date-service"),
    pathConfiger: Ember.inject.service("path-configer"),
    dataLoader: Ember.inject.service("data-loader"),
    //delFlag:true,
    customerModel: Ember.computed("customerInfo", function() {
        var model = this.get("customerInfo");
        console.log("model customerInfo", model);
        if (!model) {
            return null;
        }
        return new Changeset(model, lookupValidator(CustomerValidations), CustomerValidations);
    }),
    refreshStaffList: function() {
        var route = App.lookup('route:business.mainpage.customer-service');
        //route.refresh();
        App.lookup('controller:business.mainpage').refreshPage(route);
    },
    detailModify: Ember.computed("addMode", "showCustomer", function() {
        var addMode = this.get("addMode");
        if (addMode) {
            return true;
        } else {
            return false;
        }
        if (showCustomer) {
            return false;
        } else {
            return true;
        }
    }),
    uploadUrl: Ember.computed('property', function() {
        return this.get("pathConfiger").get("uploadUrl");
    }),
    deformation: function() {
        if ($(".cccc >div img").width() >= $(".cccc >div img").height()) {
            $(".cccc >div ").addClass("intergration_normal");
            $(".cccc >div img").css("height", "110px  ");
            $(".cccc >div img").css("width", "auto");
            console.log("this.widthcccccccccccccc==========", $(".cccc >div img").width());
            console.log('==========cccccccccccccccintergration_normal_height', $(".cccc >div img").height(), $(".cccc >div img").width());
        } else {
            $(".cccc >div ").addClass("intergration_normal");
            $(".cccc >div img").css("width", "110px ");
            $(".cccc >div img").css("height", "auto");
            console.log("this.heightccccc==========", $(".cccc >div img").height());
            console.log('==========cccccccccccccccintergration_normal', $(".cccc >div img").height(), $(".cccc >div img").width());
        }
    },
    defaultBed: Ember.computed('customerInfo.bed', function() {
        console.log("customerInfo.bed", this.get('customerInfo.bed'));
        //this.set('bedListFirst',bedList.get('firstObject'));
        //if (this.get('customerInfo.bed') && this.get('customerInfo.bed').content) {
        return this.get('customerInfo.bed');
        //}
        //return this.get('bedListFirst');
    }),
    actions: {
        dpShowAction(e) {

        },
        computedCardCode: function() {
            var cardCode = this.get("customerModel.cardCode");
            //计算出生年月日
            console.log("cardCode is", cardCode);
            var dateStr = cardCode.substring(6, 10) + "-" + cardCode.substring(10, 12) + "-" + cardCode.substring(12, 14);
            console.log("dateStr is", dateStr);
            var birthday = this.get("dateService").timeStringToTimestamp(dateStr);
            console.log("birthday isis++++++", birthday);
            var birthdayTime = this.get("dateService").timestampToTime(birthday);
            console.log("birthdayTime is what",birthdayTime);
            this.get("customerModel").set("birthdayTime", birthdayTime);
            //计算性别
            if (parseInt(cardCode.substr(16, 1)) % 2 == 1) {
                var manObj = this.get("dataLoader").findDict(Constants.sexTypeMale);
                console.log("manObj is", manObj);
                //var dictMan = this.get("store").peekRecord("dicttype", manObj.get("id"));
                //console.log("dictMan is",dictMan);
                this.get("customerModel").set("sexChange", manObj);
                this.get("customerModel").set("sex", manObj);
            } else {
                var womanObj = this.get("dataLoader").findDict(Constants.sexTypeFemale);
                //var dictWoman= this.get("store").peekRecord("dicttype", womanObj.get("id"));
                this.get("customerModel").set("sexChange", womanObj);
                this.get("customerModel").set("sex", womanObj);
            }
        },
        computedFirstCardCode: function() {
            var guardianFirstCardCode = this.get("customerModel.guardianFirstCardCode");
            console.log("guardianFirstCardCode is", guardianFirstCardCode);
            //计算性别
            if (parseInt(guardianFirstCardCode.substr(16, 1)) % 2 == 1) {
                var manObj = this.get("dataLoader").findDict(Constants.sexTypeMale);
                console.log("manObj is", manObj);
                this.get("customerModel").set("guardianSexChange", manObj);
                this.get("customerModel").set("guardianFirstGener", manObj);
            } else {
                var womanObj = this.get("dataLoader").findDict(Constants.sexTypeFemale);
                //var dictWoman= this.get("store").peekRecord("dicttype", womanObj.get("id"));
                console.log("womanObj is", womanObj);
                this.get("customerModel").set("guardianSexChange", womanObj);
                this.get("customerModel").set("guardianFirstGener", womanObj);
            }
        },
        uploadSucc: function(response) {
            var model = this.get('model');
            var res = JSON.parse(response);
            console.log("++++res+++++", res);
            this.get("customerInfo").set("headImg", res.relativePath);
            console.log("res.relativePath:", res.relativePath);
            this.get("customerInfo").save();
        },
        invalid() {
            //alert("invalid");
        },
        //修改
        detailModifyClick: function() {
            this.set('detailModify', true);
        },
        //保存按钮
        detailSaveClick: function() {
            var _self = this;
            var customerModel = this.get("customerModel");
            var mainpageController = App.lookup('controller:business.mainpage');
            console.log("++++++++++customerModel+++++++++++", this.get("customerModel"));
            customerModel.validate().then(function() {
                //alert("save   out");
                if (customerModel.get('errors.length') === 0) {
                    App.lookup('controller:business.mainpage').openPopTip("正在保存");
                    //出生日期转化为时间戳
                    // var birthdayTime = _self.get("customerInfo.birthdayString");
                    // console.log("++++++++++birthdayTime++++++++++++", birthdayTime);
                    // var birthdayDate = _self.get("dateService").timeStringToTimestamp(birthdayTime);
                    // console.log("++++++++++birthdayDate++++++++++++", birthdayDate);
                    // customerModel.set("birthday", birthdayDate);
                    //入住开始日期转化为时间戳
                    // var checkInTime = _self.get("customerInfo.checkInDateString");
                    // var checkInStamp = _self.get("dateService").timeStringToTimestamp(checkInTime);
                    // customerModel.set("checkInDate", checkInStamp);
                    //入住截止时间转化为时间戳
                    // var checkOutTime = _self.get("customerInfo.checkOutDateString");
                    // var checkOutStamp = _self.get("dateService").timeStringToTimestamp(checkOutTime);
                    // customerModel.set("checkOutDate", checkOutStamp);
                    customerModel.save().then(function() {
                        App.lookup('controller:business.mainpage').showPopTip("保存成功");
                        if (_self.get('addMode')) {
                            //alert("添加");
                            mainpageController.switchMainPage('customer-service', {});
                        }
                        _self.set('detailModify', false);
                    });
                } else {
                    customerModel.set("validFlag", Math.random());
                    alert("校验失败");
                }
            });
        },
        //暂存
        shortSaveInfo: function() {
            var _self = this;
            var customerModel = this.get("customerModel");
            var mainpageController = App.lookup('controller:business.mainpage');
            App.lookup('controller:business.mainpage').openPopTip("正在保存");
            customerModel.validate().then(function() {
                customerModel.save().then(function() {
                    App.lookup('controller:business.mainpage').showPopTip("保存成功");
                    mainpageController.switchMainPage('business-customer', {});
                    _self.sendAction('stagingAction');
                });
            });
        },
        //下一步
        next: function() {
            var _self = this;
            var customerModel = this.get("customerModel");
            customerModel.validate().then(function() {
                if (customerModel.get('errors.length') === 0) {
                    App.lookup('controller:business.mainpage').openPopTip("正在保存");
                    _self.sendAction('goNext', customerModel);
                } else {
                    customerModel.set("validFlag", Math.random());
                    alert("校验失败");
                }
            });
            // var mainpageController = App.lookup('controller:business.mainpage');
            // mainpageController.switchMainPage('try-customer', {});
        },
        //入住信息界面的取消
        remove: function() {
            var _self = this;
            var customerModel = this.get("customerModel");
            var mainpageController = App.lookup('controller:business.mainpage');
            mainpageController.switchMainPage('business-customer', {});
        },
        //取消按钮
        cancelClick: function() {
            var mainpageController = App.lookup('controller:business.mainpage');
            this.set('detailModify', false);
            //alert("详情");
            if (!this.get('addMode')) {
                this.get("customerInfo").rollbackAttributes();
                this.set("customerModel", new Changeset(this.get("customerInfo"), lookupValidator(CustomerValidations), CustomerValidations));
            }
            if (this.get("addMode")) {
                mainpageController.switchMainPage('customer-service', {
                    //flag: 'edit-add'
                });
            }
        },

        //删除按钮
        delById: function(customerInfo) {
            //this.set('showpopInvitePassModal', true);
            var _self = this;
            App.lookup('controller:business.mainpage').showConfirm("是否确定删除此基本信息", function() {
                App.lookup('controller:business.mainpage').openPopTip("正在删除");
                customerInfo.set("delStatus", 1);
                customerInfo.save().then(function() {
                    App.lookup('controller:business.mainpage').showPopTip("删除成功");
                    var mainpageController = App.lookup('controller:business.mainpage');
                    mainpageController.switchMainPage('customer-service');
                });
            });
        },
        //弹窗取消
        // invitation() {
        //     this.set('showpopInvitePassModal', false);
        // },
        //弹窗确定，删除记录
        // cancelPassSubmit(customerInfo) {
        //     App.lookup('controller:business.mainpage').openPopTip("正在删除");
        //     this.set("showpopInvitePassModal", false);
        //     customerInfo.set("delStatus", 1);
        //     customerInfo.save().then(function() {
        //         App.lookup('controller:business.mainpage').showPopTip("删除成功");
        //         var mainpageController = App.lookup('controller:business.mainpage');
        //         mainpageController.switchMainPage('customer-service');
        // });
        // },
        sexSelect: function(sexDict) {
            console.log("sexDict in", sexDict);
            this.get("customerModel").set("sex", sexDict);

            console.log("sexDict is", this.get("customerInfo.sex"));
        }, //性别字典
        abilitySelect: function(abilityDict) {
            this.get("customerInfo").set("selfCareAbility", abilityDict);
        }, //自理能力字典
        bloodSelect: function(bloodDict) {
            alert("bloodDict");
            this.get("customerInfo").set("bloodType", bloodDict);
        }, //血型字典
        religionSelect: function(religionDict) {
            this.get("customerInfo").set("religion", religionDict);
        }, //信仰字典
        educationSelect: function(educationDict) {
            this.get("customerInfo").set("educationLevel", educationDict);
        }, //文化程度字典
        maritalSelect: function(maritalDict) {
            this.get("customerInfo").set("maritalStatus", maritalDict);
        }, //婚姻字典
        GenerSelect: function(GenerDict) {
            this.get("customerInfo").set("guardianFirstGener", GenerDict);
        }, //第一监护人性别字典
        relationSelect: function(relationDict) {
            this.get("customerInfo").set("firstRelation", relationDict);
        }, //与老人关系字典
        // diningSelect: function(diningDict) {
        //     this.get("customerInfo").set("diningStandard", diningDict);
        // }, //餐饮标准字典
        // nursingSelect: function(nursingDict) {
        //     this.get("customerInfo").set("nursingGrade", nursingDict);
        // }, //护理等级字典
        // selectParent(bed) {
        //     //this.set("customerInfo").set("bed",bed);
        //     this.get('customerModel').set("bed", bed);
        //     this.set("parent", bed);
        // }, //床位信息
        nationalitySelect: function(nationalityDict) {
            this.get("customerInfo").set("nationality", nationalityDict);
        }, //民族字典
        nativeSelect: function(nativeDict) {
            this.get("customerInfo").set("customerNative", nativeDict);
        }, //籍贯字典
        // changeCheckInDateAction(date) {
        //     console.log("changeDateAction in,date", date);
        //     var stamp = this.get("dateService").getLastSecondStampOfDay(date);
        //     console.log("stamp is:" + stamp);
        //     this.set("customerModel.checkInDate", stamp);
        // },
        // changeCheckOutDateAction(date) {
        //     var stamp = this.get("dateService").getLastSecondStampOfDay(date);
        //     this.set("customerModel.checkOutDate", stamp);
        // },
        changeBirthdayAction(date) {
            var stamp = this.get("dateService").getLastSecondStampOfDay(date);
            this.set("customerModel.birthday", stamp);
        },
    }
});
